package com.lifang.imsoa.aop;


import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import com.lifang.imsoa.model.response.BusinessException;
import com.lifang.model.Response;

@Aspect
@Slf4j
public class ResponseAspect {
    /**
     * 标注该方法体为后置通知，当目标方法执行成功后执行该方法体
     */
	@Around("within(com.lifang.imsoa.facadeimpl..*)")
    public Object aroundMethod(ProceedingJoinPoint jp){
    	try{
    		return jp.proceed();
    	} catch (Throwable e) {
    		Response<?> result = new Response<Object>();
			if(e instanceof BusinessException){
				BusinessException be = (BusinessException)e;
				result.setStatus(be.getStatus());
				result.setMessage(be.getMessage());
			}else{
				log.info("其他异常,异常类名={}",e.getClass().getName());
				e.printStackTrace();
				result.setStatus(-1);
				result.setMessage("网络异常,请稍后重试");
			}
			return result;
		}
	}
	
}
